I am a bit stuck here because I can't figure out why this does not work. When i try to run the code then it says "ReferenceError: item is not defined" - but I thought that by writing "let item" I am defining it. Any idea what the problem is here ?
class Graph {
constructor() {
this.nodes = new Set();
this.edges = new Set();
}
add(m,n){
let e = new Edge (m,n)
var allEdges = new Set();
for (let item of this.edges){
allEdges.add(item.startpoint.nodeName,item.endpoint.nodeName)
};
if (allEdges.has(e.startpoint.nodeName,e.endpoint.nodeName)){
console.log("first OCL Constraint")
} else if(allEdges.has(e.endpoint.nodeName,e.startpoint.nodeName){
console.log("second OCL Constraint")}
else {
this.edges.add(item.startpoint.nodeName,item.endpoint.nodeName)};
}
};
class Edge {
constructor(startpoint,endpoint) {
this.startpoint = startpoint;
this.endpoint = endpoint;
}
};
class Node {
constructor(nodeName) {
this.nodeName = nodeName
}
};
node1 = new Node (1);
node2 = new Node (2);
node3 = new Node (3);
node4 = new Node (4);
node5 = new Node (5);
myTestGraph = new Graph();
myTestGraph.add(node1,node2);
myTestGraph.add(node2,node1);
myTestGraph.add(node3,node4);
Thanks in advance :)
The problem seems to be in the last "else", where you reference the "item" out of its scope (you declare let item inside the for block) I think the correct code for the add() method is:
add(m,n){
let item;
let e = new Edge(m,n);
var allEdges = new Set();
for (item of this.edges){allEdges.add(item.startpoint.nodeName,item.endpoint.nodeName)};
if (allEdges.has(e.startpoint.nodeName,e.endpoint.nodeName)){console.log("first OCL Constraint")}
else if(allEdges.has(e.endpoint.nodeName,e.startpoint.nodeName)){console.log("second OCL Constraint")}
else {this.edges.add(item.startpoint.nodeName,item.endpoint.nodeName)};
}